<?php
include './include/common.inc.php';
include '../admin/include/resource.class.php';
//$current_user = array(
//	'username'	=>	'admin'
//);
$resource = new resource;
$action = isset($action) ? $action : 'manage';
$forward = get_forward('/admin/resource.php');
switch ($action)
{
	case 'add':
		$have_perm = check_perms('resource02');
		if($have_perm == false)
		{
			showmessage('您没有权限进行此操作！');
		}
		if(isset($_POST['view_type']))
		{
			if(empty($res_name))
			{
				$response->text_alert('error','res_name','请填写配置名称');
			}
			if(empty($view_type))
			{
				$response->text_alert('error','view_type','请填写配置编码');
			}
			if(empty($res_sql))
			{
				$response->text_alert('error','res_sql','请填写sql');
			}
			$response->real_execute();
			if($resource->check_view_type($view_type)==true)
			{
				$response->text_alert('error','view_type','您设置的编码已占用，请修改后再提交');
				$response->real_execute();
			}
			$is_site = (isset($_POST['is_site']) && $_POST['is_site']=='1') ? "1" : "0"; 
			$res = $resource->add_resource($view_type,$res_name,$res_sql, $is_site);
			if($res === true)
			{
				$response->alert('添加成功！');
				$response->redirect($forward);
			}
			else 
			{
				$response->alert('添加失败！');
			}
			$response->execute();
		}
		else 
		{
			include template('admin', 'resource_add');
		}
		break;
	case 'edit':
		$have_perm = check_perms('resource03');
		if($have_perm == false)
		{
			showmessage('您没有权限进行此操作！');
		}
		if(isset($_POST['view_type']))
		{
			if(empty($res_name))
			{
				$response->text_alert('error','res_name','请填写配置名称');
			}
			if(empty($view_type))
			{
				$response->text_alert('error','view_type','请填写配置编码');
			}
			if(empty($res_sql))
			{
				$response->text_alert('error','res_sql','请填写sql');
			}
			$response->real_execute();
			$info = $resource->get_res_by_id($resid);
			if($resource->check_view_type($view_type)==true && $info['view_type']!=$view_type)
			{
				$response->text_alert('error','view_type','您设置的编码已占用，请修改后再提交');
				$response->real_execute();
			}
			$is_site = (isset($_POST['is_site']) && $_POST['is_site']=='1') ? "1" : "0"; 
			$res = $resource->edit_resource($resid,$view_type,$res_name,$res_sql,$is_site);
			if($res === true)
			{
				$response->alert('修改成功！');
				$response->redirect($forward);
			}
			else 
			{
				$response->alert('添加失败！');
			}
			$response->execute();
		}
		else 
		{
			$info = $resource->get_res_by_id($resid);
			if(!isset($info['resid']))
			{
				showmessage('此资源配置不存在！');
			}
			include template('admin', 'resource_edit');
		}
		break;
	case 'del':
		$have_perm = check_perms('resource04');
		if($have_perm == false)
		{
			showmessage('您没有权限进行此操作！');
		}
		$res = $resource->delete($resid);
		if($res===true)
		{
			$response->alert('删除成功！');
			$response->script('self.location=self.location');
		}
		else 
		{
			$response->alert('删除失败！');
		}
		$response->execute();
		break;
	case 'listorder':
		$have_perm = check_perms('resource05');
		if($have_perm == false)
		{
			showmessage('您没有权限进行此操作！');
		}
		if(isset($listorder) && is_array($listorder))
		{
			foreach ($listorder as $k=>$v)
			{
				$resource->edit_listorder($k,$v);
			}
			$response->alert('修改成功！');
			$response->script('self.location=self.location');
			$response->execute();
		}
		break;
	case 'manage':
	default:
		$have_perm = check_perms('resource01');
		if($have_perm == false)
		{
			showmessage('您没有权限进行此操作！');
		}
		$list = $resource->get_res();
		$forward = make_forward();
		include template('admin', 'resource');
}
?>